home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AppsToGo / Kibitz / Setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  7.8 KB  |  289 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        setup.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __GWLAYERS__
  21. #include <GWLayers.h>
  22. #endif
  23.  
  24. #ifndef __RESOURCES__
  25. #include <Resources.h>
  26. #endif
  27.  
  28. #ifndef __TOOLUTILS__
  29. #include <ToolUtils.h>
  30. #endif
  31.  
  32.  
  33. extern Cursor    *gCurrentCursor;
  34.  
  35.  
  36.  
  37.  
  38. /*****************************************************************************/
  39. /*****************************************************************************/
  40.  
  41. #ifdef applec
  42. #pragma segment Setup
  43. #endif
  44.  
  45. /*****************************************************************************/
  46. /*****************************************************************************/
  47.  
  48.  
  49.  
  50. void    DoArrangeBoard(FileRecHndl frHndl, EventRecord *event, Point clickLoc)
  51. {
  52.     short            piece, fromSq, fromRow, fromCol, toRow, toCol, toSq;
  53.     short            palettePiece, color, delta, middle, i;
  54.     Boolean            squareWasEmpty, changedPalettePiece;
  55.     Rect            paletteRect, boardRect, fromRect;
  56.     Point            releaseLoc;
  57.     ControlHandle    ctl;
  58.  
  59.     paletteRect = PaletteRect();
  60.     if (PtInRect(clickLoc, &paletteRect)) {
  61.         palettePiece = 1 + (clickLoc.h - paletteRect.left) / kBoardSqSize;
  62.         if (clickLoc.v < paletteRect.top + kBoardSqSize)
  63.             palettePiece = -palettePiece;
  64.         (*frHndl)->doc.palettePiece = palettePiece;
  65.         DrawPalette(frHndl);
  66.         SetOrigin(0, 0);
  67.         return;
  68.     }
  69.  
  70.     SetOrigin(0, 0);
  71.     clickLoc.h += 4096;
  72.  
  73.     squareWasEmpty = false;
  74.  
  75.     boardRect = BoardRect();
  76.     if (PtInRect(clickLoc, &boardRect)) {
  77.  
  78.         fromRow = (clickLoc.v - kBoardVOffset) / kBoardSqSize;
  79.         fromCol = (clickLoc.h - kBoardHOffset) / kBoardSqSize;
  80.         fromRect.top    = 1 + fromRow * kBoardSqSize;
  81.         fromRect.left   = 1 + fromCol * kBoardSqSize;
  82.         fromRect.bottom = fromRect.top  + 32;
  83.         fromRect.right  = fromRect.left + 32;
  84.  
  85.         if ((*frHndl)->doc.invertBoard) {
  86.             fromRow = 7 - fromRow;
  87.             fromCol = 7 - fromCol;
  88.         }
  89.         fromSq = START_IBNDS + 10 * fromRow + fromCol;
  90.  
  91.         piece = (*frHndl)->doc.palettePiece;
  92.         changedPalettePiece = false;
  93.         if (event->modifiers & optionKey) {
  94.             changedPalettePiece = true;
  95.             i = (*frHndl)->doc.theBoard[fromSq];
  96.             if ((i != WK) && (i != BK) && (i != EMPTY)) {
  97.                 if (i < WK) i += KSIDEPIECE;
  98.                 if (i > BK) i -= KSIDEPIECE;
  99.                 piece = (*frHndl)->doc.palettePiece = i;
  100.                 SetOrigin((kArrangeBoard * 4096), 0);
  101.                 DrawPalette(frHndl);
  102.                 SetOrigin(0, 0);
  103.             }
  104.         }
  105.  
  106.         if ((*frHndl)->doc.theBoard[fromSq] == EMPTY) {
  107.             squareWasEmpty = true;
  108.             if (piece == WP) {
  109.                 if (fromRow == 7) return;
  110.                 if (fromRow == 0) piece = WQ;
  111.             }
  112.             if (piece == BP) {
  113.                 if (fromRow == 0) return;
  114.                 if (fromRow == 7) piece = BQ;
  115.             }
  116.             (*frHndl)->doc.theBoard[fromSq]  = piece;
  117.             (*frHndl)->doc.gameIndex      = 0;
  118.             (*frHndl)->doc.numGameMoves   = 0;
  119.             (*frHndl)->fileState.docDirty = true;
  120.             ctl = (*frHndl)->doc.gameSlider;
  121.             (*ctl)->contrlMax   = 0;
  122.             (*ctl)->contrlValue = 0;
  123.  
  124.             if (fromSq == 21) (*frHndl)->doc.king[BLACK].rookMoves[QSIDE] = 0;
  125.             if (fromSq == 28) (*frHndl)->doc.king[BLACK].rookMoves[KSIDE] = 0;
  126.             if (fromSq == 91) (*frHndl)->doc.king[WHITE].rookMoves[QSIDE] = 0;
  127.             if (fromSq == 98) (*frHndl)->doc.king[WHITE].rookMoves[KSIDE] = 0;
  128.                 /* Adjust castling privileges. */
  129.  
  130.             (*frHndl)->doc.enPasMove    = (*frHndl)->doc.arngEnPasMove    = 0;
  131.             (*frHndl)->doc.enPasPawnLoc = (*frHndl)->doc.arngEnPasPawnLoc = 0;
  132.                 /* Double-pawn-push must be last change to enable en-passant. */
  133.         }
  134.  
  135.         SetCursor(*GetCursor(closedHandCursor));
  136.         gCurrentCursor = nil;
  137.  
  138.         piece = (*frHndl)->doc.theBoard[fromSq];
  139.         releaseLoc.h = releaseLoc.v = 0x4000;
  140.         if (piece) MoveThePiece(frHndl, fromSq, fromRect, clickLoc, &releaseLoc);
  141.  
  142.         if ((toRow = releaseLoc.v - kBoardVOffset) < 0) toRow = -1;
  143.         else                                            toRow /= kBoardSqSize;
  144.         if ((toCol = releaseLoc.h - kBoardHOffset) < 0) toCol = -1;
  145.         else                                            toCol /= kBoardSqSize;
  146.         if ((*frHndl)->doc.invertBoard) {
  147.             toRow = 7 - toRow;
  148.             toCol = 7 - toCol;
  149.         }
  150.         if ((toRow > -1) && (toRow < 8) && (toCol > -1) && (toCol < 8)) {
  151.             toSq = START_IBNDS + 10 * toRow + toCol;
  152.             if (toSq == fromSq) {
  153.                 if (!squareWasEmpty) {
  154.                     if (!changedPalettePiece) {
  155.                         if ((piece != WK) && (piece != BK)) {
  156.                             (*frHndl)->doc.theBoard[fromSq] = EMPTY;
  157.                             SetOrigin((kArrangeBoard * 4096), 0);
  158.                             DrawPalette(frHndl);
  159.                             SetOrigin(0, 0);
  160.                         }
  161.                     }
  162.                 }
  163.                 ImageDocument(frHndl, true);
  164.                 return;
  165.             }
  166.         }
  167.         else toSq = fromSq;
  168.  
  169.         if (((*frHndl)->doc.theBoard[toSq] == BK) || ((*frHndl)->doc.theBoard[toSq] == WK)) {
  170.             ImageDocument(frHndl, true);
  171.             return;
  172.         }
  173.  
  174.         piece = (*frHndl)->doc.theBoard[fromSq];
  175.         if ((piece == WP) && (toSq >= 91)) {
  176.             ImageDocument(frHndl, true);
  177.             return;
  178.         }
  179.         if ((piece == WP) && (toSq <= 28)) piece = WQ;
  180.         if ((piece == BP) && (toSq <= 28)) {
  181.             ImageDocument(frHndl, true);
  182.             return;
  183.         }
  184.         if ((piece == BP) && (toSq >= 91)) piece = BQ;
  185.  
  186.         (*frHndl)->doc.theBoard[toSq]    = piece;
  187.         (*frHndl)->doc.theBoard[fromSq]  = EMPTY;
  188.         (*frHndl)->doc.gameIndex      = 0;
  189.         (*frHndl)->doc.numGameMoves   = 0;
  190.         (*frHndl)->fileState.docDirty = true;
  191.         ctl = (*frHndl)->doc.gameSlider;
  192.         (*ctl)->contrlMax   = 0;
  193.         (*ctl)->contrlValue = 0;
  194.  
  195.         if ((piece == WK) || (piece == BK)) {
  196.             color = (piece < 0) ? WHITE : BLACK;
  197.             (*frHndl)->doc.king[color].kingLoc   = toSq;
  198.             (*frHndl)->doc.king[color].kingMoves = true;
  199.         }
  200.  
  201.         if ((fromSq==21) || (toSq==21)) ++(*frHndl)->doc.king[BLACK].rookMoves[QSIDE];
  202.         if ((fromSq==28) || (toSq==28)) ++(*frHndl)->doc.king[BLACK].rookMoves[KSIDE];
  203.         if ((fromSq==91) || (toSq==91)) ++(*frHndl)->doc.king[WHITE].rookMoves[QSIDE];
  204.         if ((fromSq==98) || (toSq==98)) ++(*frHndl)->doc.king[WHITE].rookMoves[KSIDE];
  205.             /* Adjust castling privileges. */
  206.  
  207.         (*frHndl)->doc.enPasMove    = (*frHndl)->doc.arngEnPasMove    = 0;
  208.         (*frHndl)->doc.enPasPawnLoc = (*frHndl)->doc.arngEnPasPawnLoc = 0;
  209.             /* Double-pawn-push must be last change to enable en-passant. */
  210.  
  211.         delta = 0;
  212.         if (piece == WP)
  213.             if ((fromSq >= 81) && (fromSq <= 88))
  214.                 delta = fromSq - toSq;
  215.         if (piece == BP)
  216.             if ((fromSq >= 31) && (fromSq <= 38))
  217.                 delta = toSq - fromSq;
  218.         if (delta == 20) {
  219.             middle = (fromSq + toSq) / 2;
  220.             if (!(*frHndl)->doc.theBoard[middle]) {
  221.                 (*frHndl)->doc.enPasMove        = middle;
  222.                 (*frHndl)->doc.arngEnPasMove    = middle;
  223.                 (*frHndl)->doc.enPasPawnLoc     = toSq;
  224.                 (*frHndl)->doc.arngEnPasPawnLoc = toSq;
  225.             }
  226.         }
  227.  
  228.         ImageDocument(frHndl, true);
  229.     }
  230. }
  231.  
  232.  
  233.  
  234. /*****************************************************************************/
  235.  
  236.  
  237.  
  238. void    DrawPalette(FileRecHndl frHndl)
  239. {
  240.     GrafPtr        curPort;
  241.     short        rr, cc, piece;
  242.     Rect        paletteRect;
  243.     CIconHandle    pieceIcon;
  244.  
  245.     GetPort(&curPort);
  246.     if (curPort->portRect.left != -4096) return;
  247.  
  248.     PenSize(1, 1);
  249.     for (rr = 1; rr < 6; ++rr) {
  250.         for (cc = -1; cc < 3; cc += 2) {
  251.             paletteRect = PaletteRect();
  252.             paletteRect.left += (rr - 1) * kBoardSqSize;
  253.             if (cc == 1) paletteRect.top += kBoardSqSize;
  254.             paletteRect.right  = paletteRect.left + 32;
  255.             paletteRect.bottom = paletteRect.top + 32;
  256.             piece = rr * cc;
  257.             if (piece == (*frHndl)->doc.palettePiece) piece += 13;
  258.             pieceIcon = ReadCIcon(263 + piece);
  259.             DrawCIconNoMask(pieceIcon, paletteRect);
  260.             InsetRect(&paletteRect, -1, -1);
  261.             FrameRect(&paletteRect);
  262.             KillCIcon(pieceIcon);
  263.         }
  264.     }
  265.     PenNormal();
  266. }
  267.  
  268.  
  269.  
  270. /*****************************************************************************/
  271.  
  272.  
  273.  
  274. Rect    PaletteRect(void)
  275. {
  276.     Rect    paletteRect;
  277.  
  278.     paletteRect.top    = 74;
  279.     paletteRect.left   = kBoardWidth + 21;
  280.     paletteRect.right  = paletteRect.left + 5 * kBoardSqSize;
  281.     paletteRect.bottom = paletteRect.top + 2 * kBoardSqSize;
  282.     OffsetRect(&paletteRect, -4096, 0);
  283.  
  284.     return(paletteRect);
  285. }
  286.  
  287.  
  288.  
  289.